package com.hr.controller.user;

import com.hr.domain.dto.LoginInFoDTO;
import com.hr.domain.vo.LoginUserVO;
import com.hr.domain.vo.UserVO;
import com.hr.service.LoginService;
import com.hr.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Slf4j
@Tag(name = "用户登录")
@AllArgsConstructor
public class LoginController {

    private final LoginService loginService;
    /**
     * 用户登录
     *
     * @param loginInFoDTO
     * @return
     */
    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public Result login(@RequestBody LoginInFoDTO loginInFoDTO) {
        log.info("用户名：{}，用户密码：{}", loginInFoDTO.getUsername(), loginInFoDTO.getPassword());
        // 使用MD5加密密码
        String encodedPassword = DigestUtils.md5DigestAsHex(loginInFoDTO.getPassword().getBytes());

        LoginUserVO loginuserInfo = loginService.login(loginInFoDTO.getUsername(), encodedPassword);
        if (loginuserInfo != null) {
            return Result.success(loginuserInfo);
        }
        return Result.error("用户名或密码错误,或该账户已背禁用！");
    }

}
